package org.example;

import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.*;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.task.api.Task;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@SpringBootApplication
public class MyFlowableApplication {
    public static void main(String[] args) throws UnknownHostException {
        ConfigurableApplicationContext application = SpringApplication.run(
                MyFlowableApplication.class, args);
        Environment env = application.getEnvironment();
        String ip = InetAddress.getLocalHost().getHostAddress();
        String port = env.getProperty("server.port");
        String property = env.getProperty("server.servlet.context-path");
        String path = property == null ? "" : property;
        log.info("\n\t" + "----------------------------------------------------------\n\t"
                + "系统启动成功! Access URLs:\n\t" + "后台地址: \thttp://" + ip + ":" + port
                + path + "/\n\t" + "文档地址: \thttp://" + ip + ":" + port + path + "/doc.html \n\t"
                + "------------------------------------------------------------");

        // 1.创建ProcessEngineConfiguration实例
        ProcessEngineConfiguration cfg = ProcessEngineConfiguration.createProcessEngineConfigurationFromResourceDefault();

        // 2.初始化ProcessEngine流程引擎实例
        ProcessEngine processEngine = cfg.buildProcessEngine();

        // 3.创建RepositoryService实例
        RepositoryService repositoryService=processEngine.getRepositoryService();
        // 4.加载流程
        Deployment deployment = repositoryService.createDeployment().addClasspathResource("holiday-request.bpmn20.xml").deploy();

        // 5.查询流程定义
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();
        System.out.println("Found process definition : "+processDefinition.getName());

        String employee = "employee";
        Integer nrOfHolidays = 5;
        String description = "description";

        RuntimeService runtimeService = processEngine.getRuntimeService();
        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("employee", employee);
        variables.put("nrOfHolidays", nrOfHolidays);
        variables.put("description", description);
        runtimeService.startProcessInstanceByKey("holiday-request", variables);

        //通过TaskService查询 manager 组的任务
        TaskService taskService=processEngine.getTaskService();
        List<Task> tasks=taskService.createTaskQuery().taskCandidateGroup("managers").list();
        System.out.println("You have " + tasks.size() + " tasks:");
        for (int i=0; i<tasks.size(); i++) {
            System.out.println((i+1) + ") " + tasks.get(i).getName());
        }

        //使用集合下标获取特定流程实例的变量,在控制台输出
        System.out.println("Which task would you like to complete?");
        int taskIndex = 1;
        Task task=tasks.get(taskIndex-1);
        Map<String,Object> processVariables=taskService.getVariables(task.getId());
        System.out.println(processVariables.get("employee") + " wants " +
                processVariables.get("nrOfHolidays") + " of holidays. Do you approve this?");

        //经理完成任务
        boolean approved= true;
        variables = new HashMap<String, Object>();
        variables.put("approved", approved);
        //任务完成，并会在离开排他网关的两条路径中，基于’approved ’流程变量选择一条
        taskService.complete(task.getId(),variables);


    }
}